﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XQ.WeChatOfficialAccount.BLL.Common;
using XQ.WeChatOfficialAccount.DAL;
using XQ.WeChatOfficialAccount.DAL.DAL;
using XQ.WeChatOfficialAccount.DAL.DAL.Business;
using XQ.WeChatOfficialAccount.DAL.Model;
using XQ.WeChatOfficialAccount.DAL.Model.Business;
using XQ.WeChatOfficialAccount.DAL.Models;

namespace XQ.WeChatOfficialAccount.BLL.Business
{
    public class ContextsBLL
    {
        ContextsDal contextsDal = new ContextsDal();
        UploadDal uploadDal = new UploadDal();

        public List<ContextsModel> GetList(ContextsSearchModel searchModel, Pagination page, TokenModel token)
        {
            return contextsDal.GetList(searchModel, page, token);
        }

        /// <summary>
        /// 根据分类ID获取文章列表
        /// </summary>
        /// <param name="searchModel"></param>
        /// <param name="page"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public List<ContextsModel> GetListByCategoryID(ContextsSearchModel searchModel, Pagination page, TokenModel token)
        {
            return contextsDal.GetList(searchModel, page, token);
        }

        /// <summary>
        /// 保存文章
        /// </summary>
        /// <param name="saveModel"></param>
        /// <param name="token"></param>
        /// <returns></returns>
        public bool SaveContexts(ContextsSaveModel saveModel, TokenModel token)
        {
            bool isSuccess = false;
            DateTime curTime = DateTime.Now;
            if (saveModel != null)
            {
                Contexts contexts = ClassHelper.ChildCopyToParent<Contexts, ContextsSaveModel>(saveModel);
                if (saveModel.id > 0)
                {
                    //修改
                    saveModel.UpdateTime = curTime;
                    saveModel.UpdateUserID = token.UID;
                    isSuccess = contextsDal.UpdateContexts(saveModel) > 0;

                }
                else
                {
                    //新增
                    contexts.AddTime = curTime;
                    contexts.AddUserID = token.UID;

                    isSuccess = contextsDal.AddModel(contexts) != null;
                }

                //保存附件
                if (saveModel.Annexs != null && saveModel.Annexs.Count > 0)
                {
                    foreach (Upload u in saveModel.Annexs)
                    {
                        u.businessID = saveModel.id;
                    }
                    uploadDal.SaveUpload(saveModel.Annexs, saveModel.id, "Contexts", "Annex");
                }
            }

            return isSuccess;
        }

        /// <summary>
        /// 获取文章详情
        /// </summary>
        /// <param name="contextsID"></param>
        /// <returns></returns>
        public ContextsSaveModel GetDetailByID(int contextsID)
        {
            Contexts contexts = contextsDal.FindModel(c => c.id == contextsID);
            ContextsSaveModel contextsModel = ClassHelper.ParentCopyToChild<Contexts, ContextsSaveModel>(contexts);
            contextsModel.Annexs = uploadDal.GetUpload("Contexts", "Annex", contextsID);

            return contextsModel;
        }

        /// <summary>
        /// 根据分类ID获取文章详情
        /// </summary>
        /// <param name="contextsID"></param>
        /// <returns></returns>
        public ContextsSaveModel GetDetailByCategoryID(int categoryID)
        {
            ContextsSaveModel contextsModel = null;
            Contexts contexts = contextsDal.FindModel(c => c.CategoryTypeID == categoryID);
            if (contexts != null)
            {
                contextsModel = ClassHelper.ParentCopyToChild<Contexts, ContextsSaveModel>(contexts);
                contextsModel.Annexs = uploadDal.GetUpload("Contexts", "Annex", contexts.id);
            }
            return contextsModel;
        }

        /// <summary>
        /// 批量删除
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public bool DeleteByIds(List<int> ids)
        {
            return contextsDal.DeleteModel(c => ids.Contains(c.id)) == ids.Count;
        }

        /// <summary>
        /// 发布文章
        /// </summary>
        /// <param name="contextsID"></param>
        /// <returns></returns>
        public bool SetContextIsSend(int contextsID)
        {
            Contexts contexts = contextsDal.FindModel(c => c.id == contextsID);
            contexts.isSend = true;
            contexts.SendTime = DateTime.Now;
            contextsDal.UpdateModel(contexts);

            return contextsDal.UpdateModel(contexts) == 1;
        }

        /// <summary>
        /// 取消发布文章
        /// </summary>
        /// <param name="contextsID"></param>
        /// <returns></returns>
        public bool SetContextIsNoSend(int contextsID)
        {
            Contexts contexts = contextsDal.FindModel(c => c.id == contextsID);
            contexts.isSend = false;
            contexts.SendTime = null;
            contextsDal.UpdateModel(contexts);

            return contextsDal.UpdateModel(contexts) == 1;
        }
    }
}
